package com.lids.test;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Des 客户端心跳检测机制
 * @Date: 2021/1/12 11:24
 */
public class HeartBeatReqHandler extends ChannelHandlerAdapter {
     private volatile ScheduledFuture<?> heartBeat;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        NettyMessage message = new NettyMessage();
        //握手成功,主动发送心跳消息
        if(message.getHeader()!= null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()){
            heartBeat =ctx.executor().scheduleAtFixedRate(new HeartBeatReqHandler.HeartBeatTask(ctx),0,5000, TimeUnit.MILLISECONDS);
        }else if(message.getHeader()!= null && message.getHeader().getType() == MessageType.HEARTBEAT_RESP.value()){
            System.out.println("Client receive server heart beat message:--->"+message);
        }else {
            ctx.fireChannelRead(msg);
        }
    }


    private class HeartBeatTask implements Runnable{
        private final ChannelHandlerContext ctx;
        public HeartBeatTask(final  ChannelHandlerContext ctx){
            this.ctx =ctx;
        }

        @Override
        public void run() {
              NettyMessage heatBeat = buildHeatBeat();
              System.out.println("Client send heart beat message to server:--->"+heartBeat);
              ctx.writeAndFlush(heatBeat);
        }

        private NettyMessage buildHeatBeat() {
            NettyMessage message = new NettyMessage();
            Header header = new Header();
            header.setType(MessageType.HEARTBEAT_REQ.value());
            message.setHeader(header);
            return message;
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
         if(heartBeat != null){
             heartBeat.cancel(true);
             heartBeat = null;
         }
         ctx.fireExceptionCaught(cause);
    }
}
